var Skink; if (!Skink) Skink = {};

/***********
Skink.Intercept
************/
if (!Skink.Intercept) Skink.Intercept = {};

// ***************************************************************
// Event delegation handling
// ***************************************************************
Skink.Intercept.init = function() {
	air.trace('Initialising intercepts...');

	$('body').intercept('mouseover', {
			'.status-action[title]':function(e) {
				var tt = new Skink_Tooltip({
					'e'		:e,
					'str'	:$(this).attr('title'),
					// 'previewurl':href,
				});
				tt.show();

			},
			'.user-screen-name[title]':function(e) {
				var tt = new Skink_Tooltip({
					'e'		:e,
					'el'	:this,
					'str'	:$(this).attr('title'),
					// 'previewurl':href,
				});
				tt.show();

				// Skink.UI.showTooltip(this, $(this).attr('title'));
			},
			'a.in-reply-to':function(e) {
				var tt = new Skink_Tooltip({
					'e'		:e,
					'el'	:this,
					'str'	:$(this).attr('title'),
					'reply_status_id':$(this).attr('status-id'),
					'reply_screen_name':$(this).attr('screen-name'),
				});
				tt.show();
				// Skink.UI.showTooltip(this, $(this).attr('title'));
			},
			'.user-image[title]':function(e) {
				var tt = new Skink_Tooltip({
					'e'		:e,
					'el'	:this,
					'str'	:$(this).attr('title'),
					// 'previewurl':href,
				});
				tt.show();


				// Skink.UI.showTooltip(this, $(this).attr('title'));
			},
			'a.inline-link':function(e) {
				var href = $(this).attr('href');
				// Skink.UI.showTooltip(this, "Open "+href+" in a browser window", href);
				var tt = new Skink_Tooltip({
					'e'		:e,
					'el'	:this,
					'str'	:"Open "+href+" in a browser window",
					'previewurl':href,
				});
				tt.show();

				// Skink.UI.showTooltip(this, "Open "+href+" in a browser window", href);
			},
			'a .highlight':function(e) {
				if ($(this).parents('a').attr('href')) {
					var href = $(this).parents('a').attr('href');
					var tt = new Skink_Tooltip({
						'e'		:e,
						'el'	:this,
						'str'	:"Open "+href+" in a browser window",
						'previewurl':href,
					});
					tt.show();
					// Skink.UI.showTooltip(this, "Open "+href+" in a browser window", href);
				}
			},
			'a[title]':function(e) {
				var tt = new Skink_Tooltip({
					'e'		:e,
					'el'	:this,
					'str'	:$(this).attr('title'),
					'reply_status_id':$(this).attr('status-id'),
					'reply_screen_name':$(this).attr('screen-name'),
					'previewurl':$(this).attr('href'),
				});
				tt.show();

				// Skink.UI.showTooltip(this, $(this).attr('title'), $(this).attr('href'));

			},
			'a[user-screen_name]':function(e) {
				var tt = new Skink_Tooltip({
					'e'		:e,
					'el'	:this,
					'str'	:$(this).attr('title'),
					'previewurl':$(this).attr('href'),
				});
				tt.show();
			},
			'input[title]':function(e) {
				var tt = new Skink_Tooltip({
					'e'		:e,
					'el'	:this,
					'str'	:$(this).attr('title'),
					// 'previewurl':href,
				});
				tt.show();
			},
			'.directory-user-followstatus':function(e) {
				var tt = new Skink_Tooltip({
					'e'		:e,
					'el'	:this,
					'str'	:$(this).attr('title'),
					// 'previewurl':href,
				});
				tt.show();
			},
		})

		.intercept('mouseout', {
			'[title]':function(e) {
				Skink.UI.hideTooltips();
			},
			'#tooltip':function(e) {
				Skink.UI.hideTooltips();
			}
		})

		.intercept('click', {
			'#filter-friends':function(e) {
				this.select();
			},
			'#filter-user':function(e) {
				this.select();
			},
			'#filter-public':function(e) {
				this.select();
			},
			'#refresh-friends':function(e) {
				Skink.UI.reloadCurrentTab(true);
				Skink.restartReloadTimer();
			},
			'#refresh-user':function(e) {
				Skink.UI.reloadCurrentTab(true);
				Skink.restartReloadTimer();
			},
			'#refresh-public':function(e) {
				Skink.UI.reloadCurrentTab(true);
				Skink.restartReloadTimer();
			},
			'#markread-friends':function(e) {
				Skink.UI.markCurrentTimelineAsRead();
			},
			'#markread-user':function(e) {
				Skink.UI.markCurrentTimelineAsRead();
			},
			'#markread-public':function(e) {
				Skink.UI.markCurrentTimelineAsRead();
			},
			'#prefs-open-folder':function(e) {
				Skink.Sys.openAppStorageFolder();
			},
			'#friendslist-showfriends':function(e) {
				$('#timeline-followers').fadeOut();
				$('#timeline-friendslist').fadeIn();
			},
			'#friendslist-showfollowers':function(e) {
				$('#timeline-friendslist').fadeOut();
				$('#timeline-followers').fadeIn();
			},
			'#search-go':function(e) {
				Skink.Section.search.build();
			},
			'#search-help':function(e) {
				openInBrowser('http://search.twitter.com/operators');
			},
			'#mainMenu-help':function(e) {
				Skink.UI.showHelp();
			},
			'#mainMenu-prefs':function(e) {
				Skink.UI.showPrefs();
			},
			'#mainMenu-about':function(e) {
				Skink.UI.showAbout();
			},
			'#mainmenu-shortenLink':function(e) {
				Skink.UI.showShortLink();
			},

			'#irt-dismiss':function(e) {
				Skink.UI.clearPostIRT();
				$('#entrybox').focus();
			},

			// prefs buttons handlers
			'#prefs-autosetrefresh-button':function(e) {
				Skink.Data.getRateLimitInfo( Skink.Prefs.setRateLimit );
			},

			// user context menu handlers
			'#userContextMenu-viewProfile':function(e) {
				Skink.Sys.openInBrowser(Skink.Prefs.get('twitter-base-url')+$(this).attr('user-screen_name'))
			},
			'#userContextMenu-follow':function(e) {
				Skink.Data.followUser($(this).attr('user-screen_name'));
			},
			'#userContextMenu-unfollow':function(e) {
				Skink.Data.stopFollowingUser($(this).attr('user-screen_name'));
			},
			'#userContextMenu-sendReply':function(e) {
				Skink.UI.prepReply($(this).attr('user-screen_name'));
			},
			'#userContextMenu-sendDM':function(e) {
				Skink.UI.prepDirectMessage($(this).attr('user-screen_name'));
			},
			'#userContextMenu-searchForUser':function(e) {
				var screen_name = $(this).attr('user-screen_name');
				var search_str = "from:"+screen_name+" OR to:"+screen_name;

				$('#search-for').val(search_str);
			    Skink.Section.search.build();
			    Skink.UI.showTab(3);
			},
			'#userContextMenu-filterByUser':function(e) {
				var screen_name = $(this).attr('user-screen_name');
				$('#filter-friends').val(screen_name);
				$('#filter-friends').trigger('keyup');
			    Skink.UI.showTab(0);
			},

			'a[href]':function(e) {
				var url = $(this).attr('href');
				openInBrowser(url);
				return false;
			},
			'.user-screen-name':function(e) {
				var url = Skink.Prefs.get('twitter-base-url')+$(this).attr('user-screen_name');
				openInBrowser(url);
			},
			'.user-image':function(e) {
				var url = Skink.Prefs.get('twitter-base-url')+$(this).attr('user-screen_name');
				openInBrowser(url);
			},
			'.status-action-fav':function(e) {
				var entryid = $(this).attr('entry-id')
				var element = Skink.UI.getElementFromStatusId(entryid)
				if ($(element).hasClass('favorited')) {
					Skink.Data.makeNotFavorite(entryid);
				}
				else {
					Skink.Data.makeFavorite(entryid);
				}
			},
			'.status-action-retweet':function(e) {
				Skink.UI.prepRetweet($(this).attr('entry-id'));
			},
			'.status-action-dm':function(e) {
				//Skink.UI.prepDirectMessage($(this).attr('user-screen_name'));

				Skink.UI.statusBar("Translating...")
				Skink.UI.showLoading();

				var id         = $(this).attr('entry-id');
                var tweetText  = Skink.UI.extractTweet(id);
                var sourceLang = Skink.Translate.detect(tweetText);

				if (sourceLang !== Skink.Prefs.get('search-base-lang')) {
					var reply = Skink.Translate.translate(tweetText, sourceLang);
					Skink.UI.updateTranslation(id, sourceLang, reply);
				} else {
					Skink.UI.updateTranslation(id, sourceLang, "");
                }

				Skink.UI.hideLoading();
				Skink.UI.statusBar("");
			},
			'.status-action-reply':function(e) {
				Skink.UI.prepReply($(this).attr('user-screen_name'), $(this).attr('entry-id'));
			},
			'.status-action-del':function(e) {
				Skink.Data.destroyStatus($(this).attr('entry-id'))
			},
			'.directory-action-follow':function(e) {
				Skink.Data.followUser($(this).attr('user-screen_name'));
			},
			'.directory-action-unfollow':function(e) {
				Skink.Data.stopFollowingUser($(this).attr('user-screen_name'));
			},
			'.directory-user-location': function(e) {
				Skink.UI.showLocationOnMap($(this).text());
			},
			'.timeline-entry':function(e) {
				Skink.UI.selectEntry(this);
			},
			'.timeline-entry *':function(e) { // this one needs to be last so the more specific ones above take precedence
				// $('div.timeline-entry.ui-selected').removeClass('ui-selected').addClass('read');
				var entry = $(this).parents('.timeline-entry').get(0);
				Skink.UI.selectEntry(entry);
				// entry.addClass('ui-selected');
			},
			'a .highlight':function(e) {
				if ($(this).parents('a').attr('href')) {
					openInBrowser($(this).parents('a').attr('href'));
				}
				return false;
			},
			'a':function(e) {
				Skink.dump(this.outerHTML);
				if ($(this).attr('href')) {
          // hashtags
				  if($(this).text().match(/^#/)) {
				    $('#search-for').val($(this).text());
				    Skink.Section.search.build();
				    Skink.UI.showTab(3);
				  }
				  else
					  openInBrowser($(this).attr('href'));
				}
				return false;
			},

			'document':function(e){
				$('#userContextMenu').hide();
			},

			// '#header-label':function(e) {
			//	Skink.UI.showMainMenu($(this));
			// },
		})
		.intercept('contextmenu', {
			// 'div.timeline-entry .user, div.timeline-entry .user-image, div.timeline-entry .user-screen-name':function(e) {
			'.user,.user-image,.user-screen-name,a[user-screen_name]':function(e) {
				// air.trace(this.outerHTML);
				var screen_name = $(this).attr('user-screen_name');
				Skink.UI.showUserContextMenu($(this), screen_name);
			},
			'a[href]':function(e) {
				var url = $(this).attr('href');
				Skink.UI.showLinkContextMenu($(this), url);
			},
		})
		.intercept('keyup', {
			'#filter-friends':function(e) {
				// air.trace('filter:'+this.outerHTML+"\n"+$(this).val());

				Skink.Section.friends.filter( $(this).val() );
			},
			'#filter-user':function(e) {
				Skink.Section.user.filter( $(this).val() );
			},
			'#filter-public':function(e) {
				Skink.Section.public.filter( $(this).val() );
			}
		})



	// end intercept


	/*
		Normal bindings where intercept doesn't work
	*/


	$('#entrybox').focus(function(e) {
			Skink.UI.showEntryboxTip();
			$('#entrystats').fadeIn('fast');
		})
		.blur(function(e) {
			Skink.UI.resetStatusBar();
			$("body").focus();
			$('#entrystats').fadeOut('fast');
			return false;
		});


	/*
		Set-up invoke event for command-line processing
	*/
	air.NativeApplication.nativeApplication.addEventListener(air.InvokeEvent.INVOKE, invokeHandler);

	function invokeHandler (e) {
		air.trace('Invoke args: '+e.arguments);
	}

	/*
		Set-up drag and drop events
	*/
	var target = document.getElementById('container');
	target.addEventListener("dragenter", dragEnterOverHandler);
	target.addEventListener("dragover", dragEnterOverHandler);
	target.addEventListener("drop", dropHandler);

    function dragEnterOverHandler(event){
        event.preventDefault();
    }

    function dropHandler(event){
		event.preventDefault();
    }

};